package com.unitedvision.persistence.domain;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.unitedvision.core.domain.Customer;
import com.unitedvision.core.domain.CustomerType;
import com.unitedvision.core.domain.GeneralType;
import com.unitedvision.core.exception.WrongFormatException;

@Entity
@Table (name = "customer")
public class CustomerEntity implements GeneralType {
	private int id;
	private String name;
	private CustomerTypeEntity type;
	private AddressValue address;
	private ContactValue contact;
	private List<CompanyEntity> companies;
	private List<CompanyCustomerEntity> companyCustomers;
	
	public CustomerEntity() {
		super();
	}

	public CustomerEntity(String name, CustomerTypeEntity type, AddressValue address, ContactValue contact) {
		super();
		this.name = name;
		this.type = type;
		this.address = address;
		this.contact = contact;
	}
	
	public CustomerEntity(Customer customer) throws WrongFormatException {
		super();
		this.name = customer.getName();
		this.type = CustomerTypeEntity.create(customer.getType());
		this.address = AddressValue.create(customer.getAddress());
		this.contact = ContactValue.create(customer.getContact());
	}

	@Id @GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column (name = "name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToOne (fetch = FetchType.EAGER)
	@JoinColumn (name = "type", referencedColumnName = "id")
	public CustomerTypeEntity getType() {
		return type;
	}

	public void setType(CustomerTypeEntity type) {
		this.type = type;
	}

	@Embedded
	public AddressValue getAddress() {
		return address;
	}

	public void setAddress(AddressValue addressValue) {
		this.address = addressValue;
	}

	@Embedded
	public ContactValue getContact() {
		return contact;
	}

	public void setContact(ContactValue contactValue) {
		this.contact = contactValue;
	}

	@OneToMany (targetEntity = CompanyEntity.class, mappedBy = "customer", fetch = FetchType.LAZY)
	public List<CompanyEntity> getCompanies() {
		return companies;
	}

	public void setCompanies(List<CompanyEntity> companyEntities) {
		this.companies = companyEntities;
	}

	@OneToMany (targetEntity = CompanyCustomerEntity.class, mappedBy = "customer", fetch = FetchType.LAZY)
	public List<CompanyCustomerEntity> getCompanyCustomers() {
		return companyCustomers;
	}

	public void setCompanyCustomers(List<CompanyCustomerEntity> companyCustomers) {
		this.companyCustomers = companyCustomers;
	}

	@Transient
	public String getEmail() {
		if (contact == null)
			return "Data kontak belum dimasukkan";
		return contact.getEmail();
	}
	
	public void setEmail(String email) throws WrongFormatException {
		if (contact == null)
			contact = new ContactValue();
		contact.setEmail(email);
	}
	
	@Transient
	public String getPhone() {
		if (contact == null)
			return "Data kontak belum dimasukkan";
		return contact.getPhone();
	}

	public void setPhone(String phone) throws WrongFormatException {
		if (contact == null)
			contact = new ContactValue();
		contact.setPhone(phone);
	}
	
	public Customer toCustomer() {
		return new Customer(id, name, new CustomerType(name), address.toAddress(), contact.toContact());
	}
	
	public static CustomerEntity create(String name, CustomerTypeEntity type, AddressValue address, ContactValue contact) {
		return new CustomerEntity(name, type, address, contact);
	}
	
	public static CustomerEntity create(Customer customer) throws WrongFormatException {
		return new CustomerEntity(customer);
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((address == null) ? 0 : address.hashCode());
		result = prime * result + ((contact == null) ? 0 : contact.hashCode());
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		
		CustomerEntity other = (CustomerEntity) obj;
		
		if (address == null) {
			if (other.address != null)
				return false;
		} else if (!address.equals(other.address))
			return false;
		if (contact == null) {
			if (other.contact != null)
				return false;
		} else if (!contact.equals(other.contact))
			return false;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (type != other.type)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "CustomerEntity [id=" + id + ", name=" + name + ", type=" + type
				+ ", address=" + address + ", contact=" + contact + "]";
	}
}
